home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 18 gdiplus / gdidemo / imaging.vb < prev    next >
Encoding:
Text File  |  2001-10-18  |  15.9 KB  |  425 lines

  1. Imports System.Drawing.Imaging
  2.  
  3. Public Class Imaging
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  28.     Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
  29.     Friend WithEvents mnuLoad As System.Windows.Forms.MenuItem
  30.     Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
  31.     Friend WithEvents mnuSave As System.Windows.Forms.MenuItem
  32.     Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
  33.     Friend WithEvents mnuClipping As System.Windows.Forms.MenuItem
  34.     Friend WithEvents mnuRotate As System.Windows.Forms.MenuItem
  35.     Friend WithEvents mnuTransparentBitmaps As System.Windows.Forms.MenuItem
  36.     Friend WithEvents mnuIcons As System.Windows.Forms.MenuItem
  37.     Friend WithEvents mnuMetafiles As System.Windows.Forms.MenuItem
  38.     Friend WithEvents mnuDrawMetafile As System.Windows.Forms.MenuItem
  39.  
  40.     'Required by the Windows Form Designer
  41.     Private components As System.ComponentModel.Container
  42.  
  43.     'NOTE: The following procedure is required by the Windows Form Designer
  44.     'It can be modified using the Windows Form Designer.  
  45.     'Do not modify it using the code editor.
  46.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  47.         Me.mnuClipping = New System.Windows.Forms.MenuItem()
  48.         Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
  49.         Me.mnuLoad = New System.Windows.Forms.MenuItem()
  50.         Me.MainMenu1 = New System.Windows.Forms.MainMenu()
  51.         Me.MenuItem1 = New System.Windows.Forms.MenuItem()
  52.         Me.mnuSave = New System.Windows.Forms.MenuItem()
  53.         Me.mnuRotate = New System.Windows.Forms.MenuItem()
  54.         Me.mnuTransparentBitmaps = New System.Windows.Forms.MenuItem()
  55.         Me.mnuIcons = New System.Windows.Forms.MenuItem()
  56.         Me.mnuMetafiles = New System.Windows.Forms.MenuItem()
  57.         Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
  58.         Me.mnuDrawMetafile = New System.Windows.Forms.MenuItem()
  59.         '
  60.         'mnuClipping
  61.         '
  62.         Me.mnuClipping.Index = 2
  63.         Me.mnuClipping.Text = "Clipping and scaling"
  64.         '
  65.         'mnuLoad
  66.         '
  67.         Me.mnuLoad.Index = 0
  68.         Me.mnuLoad.Text = "Load from file"
  69.         '
  70.         'MainMenu1
  71.         '
  72.         Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
  73.         '
  74.         'MenuItem1
  75.         '
  76.         Me.MenuItem1.Index = 0
  77.         Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuLoad, Me.mnuSave, Me.mnuClipping, Me.mnuRotate, Me.mnuTransparentBitmaps, Me.mnuIcons, Me.mnuMetafiles, Me.mnuDrawMetafile})
  78.         Me.MenuItem1.Text = "Examples"
  79.         '
  80.         'mnuSave
  81.         '
  82.         Me.mnuSave.Index = 1
  83.         Me.mnuSave.Text = "Save to File"
  84.         '
  85.         'mnuRotate
  86.         '
  87.         Me.mnuRotate.Index = 3
  88.         Me.mnuRotate.Text = "Mirror, Rotate, and Skew"
  89.         '
  90.         'mnuTransparentBitmaps
  91.         '
  92.         Me.mnuTransparentBitmaps.Index = 4
  93.         Me.mnuTransparentBitmaps.Text = "Semi-transparent bitmaps"
  94.         '
  95.         'mnuIcons
  96.         '
  97.         Me.mnuIcons.Index = 5
  98.         Me.mnuIcons.Text = "Icons"
  99.         '
  100.         'mnuMetafiles
  101.         '
  102.         Me.mnuMetafiles.Index = 6
  103.         Me.mnuMetafiles.Text = "Create Metafile"
  104.         '
  105.         'SaveFileDialog1
  106.         '
  107.         Me.SaveFileDialog1.FileName = "doc1"
  108.         '
  109.         'mnuDrawMetafile
  110.         '
  111.         Me.mnuDrawMetafile.Index = 7
  112.         Me.mnuDrawMetafile.Text = "Draw Metafile"
  113.         '
  114.         'Imaging
  115.         '
  116.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  117.         Me.ClientSize = New System.Drawing.Size(664, 365)
  118.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  119.         Me.Menu = Me.MainMenu1
  120.         Me.Name = "Imaging"
  121.         Me.Text = "Imaging"
  122.  
  123.     End Sub
  124.  
  125. #End Region
  126.  
  127.     Private Sub mnuLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuLoad.Click
  128.         With OpenFileDialog1
  129.             .Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.exif;*.tif;*.tiff"
  130.             If .ShowDialog = DialogResult.OK Then
  131.                 Dim bmp As Bitmap = Bitmap.FromFile(.FileName)
  132.                 Dim g As Graphics = Graphics.FromImage(bmp)
  133.                 g.FillRectangle(Brushes.Red, 20, 20, 100, 100)
  134.                 g.Dispose()
  135.  
  136.                 Dim gr As Graphics = Me.CreateGraphics
  137.  
  138.                 gr.DrawImage(bmp, 0, 0)
  139.                 bmp.Dispose()
  140.                 gr.Dispose()
  141.             End If
  142.         End With
  143.     End Sub
  144.  
  145.     Private Sub mnuSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSave.Click
  146.         Dim bmp As Bitmap
  147.         Dim gr As Graphics = Me.CreateGraphics
  148.  
  149.         With OpenFileDialog1
  150.             .Title = "Select source image"
  151.             .Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.exif;*.tif;*.tiff"
  152.             If .ShowDialog <> DialogResult.OK Then Exit Sub
  153.  
  154.             bmp = New Bitmap(.FileName)
  155.             gr.DrawImage(bmp, 0, 0)
  156.         End With
  157.  
  158.         With SaveFileDialog1
  159.             .Title = "Select target image and format"
  160.             .Filter = "Bitmap|*.bmp|GIF|*.gif|TIFF|*.tif"
  161.             .OverwritePrompt = True
  162.             If .ShowDialog = DialogResult.OK Then
  163.                 Select Case System.IO.Path.GetExtension(.FileName).ToUpper
  164.                     Case ".BMP"
  165.                         bmp.Save(.FileName, ImageFormat.Bmp)
  166.                     Case ".GIF"
  167.                         bmp.Save(.FileName, ImageFormat.Gif)
  168.                     Case ".TIF"
  169.                         bmp.Save(.FileName, ImageFormat.Tiff)
  170.                     Case Else
  171.                         MessageBox.Show("Unrecognized extension", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  172.                 End Select
  173.             End If
  174.         End With
  175.  
  176.         bmp.Dispose()
  177.         gr.Dispose()
  178.     End Sub
  179.  
  180.     Private Sub mnuClipping_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuClipping.Click
  181.         ' Wait until the menu closes.
  182.         Threading.Thread.Sleep(400)
  183.  
  184.         Dim gr As Graphics = Me.CreateGraphics
  185.         gr.Clear(Color.White)
  186.         Dim bmp As New Bitmap("logo.bmp")
  187.  
  188.         ' Copy only the left half of the image.
  189.         gr.DrawImage(bmp, 20, 20, New RectangleF(0, 0, bmp.Width / 2, bmp.Height), GraphicsUnit.Pixel)
  190.  
  191.         ' Create a destination rectangle 3 times as wide and twice as tall.
  192.         Dim rect As New RectangleF(20, 120, bmp.Width * 3, bmp.Height * 2)
  193.         ' Draw the enlarged bitmap.
  194.         gr.DrawImage(bmp, rect)
  195.  
  196.         ' Move the destination rectangle down.
  197.         Dim destrect As New RectangleF(20, 320, bmp.Width * 1.5, bmp.Height)
  198.         ' Create a rectangle corresponding to the top-left quarter of the image.
  199.         Dim sourceRect As New RectangleF(0, 0, bmp.Width / 2, bmp.Height / 2)
  200.         ' Draw the portion of bitmap in the destination rectangle.
  201.         gr.DrawImage(bmp, destRect, sourceRect, GraphicsUnit.Pixel)
  202.  
  203.         bmp.Dispose()
  204.         gr.Dispose()
  205.     End Sub
  206.  
  207.     Private Sub mnuRotate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuRotate.Click
  208.         ' Wait until the menu closes.
  209.         Threading.Thread.Sleep(400)
  210.  
  211.         Dim gr As Graphics = Me.CreateGraphics
  212.         gr.Clear(Color.White)
  213.         Dim bmp As New Bitmap("logo.bmp")
  214.  
  215.         DrawFlipImage(gr, bmp, 100, 20, False, False)
  216.         DrawFlipImage(gr, bmp, 300, 20, True, False)
  217.         DrawFlipImage(gr, bmp, 100, 120, False, True)
  218.         DrawFlipImage(gr, bmp, 300, 120, True, True)
  219.  
  220.         DrawRotateImage(gr, bmp, 100, 220, 45)
  221.         DrawRotateImage(gr, bmp, 300, 220, 90)
  222.         DrawRotateImage(gr, bmp, 500, 220, 135)
  223.  
  224.         DrawSkewImage(gr, bmp, 100, 400, -50, 0)
  225.         DrawSkewImage(gr, bmp, 300, 400, 0, 50)
  226.         DrawSkewImage(gr, bmp, 500, 400, -50, 50)
  227.  
  228.         bmp.Dispose()
  229.         gr.Dispose()
  230.     End Sub
  231.  
  232.     Private Sub mnuTransparentBitmaps_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTransparentBitmaps.Click
  233.         ' Wait until the menu closes.
  234.         Threading.Thread.Sleep(400)
  235.  
  236.         Dim gr As Graphics = Me.CreateGraphics
  237.         gr.Clear(Color.White)
  238.         Dim bmp As New Bitmap("logo.bmp")
  239.  
  240.         ' Create a grid of lines.
  241.         Dim p As New Pen(Color.Green, 4)
  242.         Dim x, y As Single
  243.         For x = 0 To Me.ClientSize.Width Step 40
  244.             gr.DrawLine(p, x, 0, x, Me.ClientSize.Height)
  245.         Next
  246.         For y = 0 To Me.ClientSize.Height Step 40
  247.             gr.DrawLine(p, 0, y, Me.ClientSize.Width, y)
  248.         Next
  249.  
  250.         ' Create a clone bitmap and make it transparent.
  251.         Dim bmp2 As Bitmap = CType(bmp.Clone(), Bitmap)
  252.         bmp2.MakeTransparent(Color.FromArgb(140, 195, 247))
  253.         gr.DrawImage(bmp2, 20, 20)
  254.  
  255.         ' Create a semi-transparent bitmap.
  256.         Dim transparency As Single = 0.8
  257.         ' Create a 5x5 matrix with the transparency value in position (4,4).
  258.         Dim values()() As Single = {New Single() {1, 0, 0, 0, 0}, _
  259.             New Single() {0, 1, 0, 0, 0}, _
  260.             New Single() {0, 0, 1, 0, 0}, _
  261.             New Single() {0, 0, 0, transparency, 0}, _
  262.             New Single() {0, 0, 0, 0, 1}}
  263.         ' Use the matrix to initialize a new colorMatrix object.
  264.         Dim colMatrix As New ColorMatrix(values)
  265.         ' Create an ImageAttributes object and assign its color matrix.
  266.         Dim imageAttr As New ImageAttributes()
  267.         imageAttr.SetColorMatrix(colMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
  268.         ' Draw the bitmap using the specified color matrix.
  269.         gr.DrawImage(bmp, New Rectangle(200, 20, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
  270.  
  271.         ' Change the alpha blending value of individual pixels.
  272.         Dim bmp3 As Bitmap = bmp.Clone
  273.         For x = 0 To bmp3.Width - 1
  274.             For y = 0 To bmp3.Height - 1
  275.                 Dim oldColor = bmp3.GetPixel(x, y)
  276.                 Dim newColor As Color = Color.FromArgb(x / bmp3.Width * 256, oldColor)
  277.                 bmp3.SetPixel(x, y, newColor)
  278.             Next
  279.         Next
  280.         gr.DrawImage(bmp3, 400, 20)
  281.  
  282.         bmp.Dispose()
  283.         bmp2.Dispose()
  284.         bmp3.Dispose()
  285.         gr.Dispose()
  286.  
  287.     End Sub
  288.  
  289.     Private Sub mnuIcons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuIcons.Click
  290.         ' Wait until the menu closes.
  291.         Threading.Thread.Sleep(400)
  292.  
  293.         Dim gr As Graphics = Me.CreateGraphics
  294.         gr.Clear(Color.White)
  295.  
  296.         ' Load the icon from file.
  297.         Dim icon As New Icon("W95MBX01.ICO")
  298.         ' Draw the icon in its original size.
  299.         gr.DrawIcon(icon, 20, 20)
  300.         ' Draw the icon with a 400% zoom.
  301.         gr.DrawIcon(icon, New Rectangle(100, 20, icon.Width * 4, icon.Height * 4))
  302.  
  303.         Dim bmp As Bitmap = icon.ToBitmap
  304.         bmp.MakeTransparent(Color.Red)
  305.         gr.DrawImage(bmp, 20, 200)
  306.  
  307.         icon.Dispose()
  308.         bmp.Dispose()
  309.         gr.Dispose()
  310.     End Sub
  311.  
  312.     Private Sub mnuMetafiles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuMetafiles.Click
  313.         ' Wait until the menu closes.
  314.         Threading.Thread.Sleep(400)
  315.  
  316.         Dim gr As Graphics = Me.CreateGraphics
  317.  
  318.         ' Create a new metafile object, with same features as the form's Graphics.
  319.         Dim mf As New Metafile("sample.emf", gr.GetHdc)
  320.         ' Retrieve the metafile's internal Graphics object.
  321.         Dim mfgr As Graphics = Graphics.FromImage(mf)
  322.         ' Draw some graphics and text on the metafile's Graphics.
  323.         mfgr.DrawLine(Pens.Blue, 20, 20, 200, 200)
  324.         mfgr.DrawRectangle(Pens.Red, 50, 50, 200, 120)
  325.         mfgr.FillEllipse(Brushes.Green, 70, 20, 200, 120)
  326.         Dim fnt As New Font("Arial", 20, FontStyle.Bold Or FontStyle.Italic)
  327.         mfgr.DrawString("Test String ", fnt, Brushes.Yellow, 120, 100)
  328.  
  329.         ' End the drawing on the metafile's surface.
  330.         fnt.Dispose()
  331.         mfgr.Dispose()
  332.         mfgr = Nothing
  333.         mf.Dispose()
  334.         mf = Nothing
  335.  
  336.         ' We must comment this statement out, otherwise we get an error
  337.         '    "The object is in use elsewhere"
  338.         ' gr.Dispose()
  339.  
  340.         MessageBox.Show("The Sample.emf file has been created")
  341.     End Sub
  342.  
  343.     Private Sub mnuDrawMetafile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDrawMetafile.Click
  344.         ' Wait until the menu closes.
  345.         Threading.Thread.Sleep(400)
  346.  
  347.         Dim gr As Graphics = Me.CreateGraphics
  348.         gr.Clear(Color.White)
  349.  
  350.         ' Read the metafile from file.
  351.         Dim mf As New Metafile("sample.emf")
  352.         ' Draw the metafile twice.
  353.         gr.DrawImage(mf, 0, 0)
  354.         gr.DrawImage(mf, 300, 100)
  355.  
  356.         mf.Dispose()
  357.         gr.Dispose()
  358.  
  359.     End Sub
  360. End Class
  361.  
  362.  
  363.  
  364.  
  365. Module GraphicEffects
  366.     Sub DrawFlipImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal flipX As Boolean, ByVal flipY As Boolean)
  367.         ' Start with values of parallelogram's vertex as if no flipping occurs.
  368.         Dim x0 As Single = x
  369.         Dim y0 As Single = y
  370.         Dim x1 As Single = x + bmp.Width
  371.         Dim y1 As Single = y
  372.         Dim x2 As Single = x
  373.         Dim y2 As Single = y + bmp.Height
  374.  
  375.         ' Account for horizontal flipping.
  376.         If flipX Then
  377.             x0 = x + bmp.Width
  378.             x1 = x
  379.             x2 = x0
  380.         End If
  381.         ' Account for vertical flipping.
  382.         If flipY Then
  383.             y0 = y + bmp.Height
  384.             y1 = y0
  385.             y2 = y
  386.         End If
  387.         ' Create the points array.
  388.         Dim points() As Point = {New Point(x0, y0), New Point(x1, y1), New Point(x2, y2)}
  389.         ' Draw the flipped image
  390.         gr.DrawImage(bmp, points)
  391.     End Sub
  392.  
  393.     Sub DrawRotateImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal angle As Single)
  394.         ' Convert the angle in degrees.
  395.         angle = angle / (180 / Math.PI)
  396.         ' Find the position of (x1,y1) and (x2,y2).
  397.         Dim x1 As Single = x + bmp.Width * Math.Cos(angle)
  398.         Dim y1 As Single = y + bmp.Width * Math.Sin(angle)
  399.         Dim x2 As Single = x - bmp.Height * Math.Sin(angle)
  400.         Dim y2 As Single = y + bmp.Height * Math.Cos(angle)
  401.  
  402.         ' Create the points array.
  403.         Dim points() As Point = {New Point(x, y), New Point(x1, y1), New Point(x2, y2)}
  404.         ' Draw the rotated image
  405.         gr.DrawImage(bmp, points)
  406.     End Sub
  407.  
  408.     Sub DrawSkewImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal dx As Single, ByVal dy As Single)
  409.         ' Find the position of (x1,y1) and (x2,y2).
  410.         Dim x1 As Single = x + bmp.Width
  411.         Dim y1 As Single = y + dy
  412.         Dim x2 As Single = x + dx
  413.         Dim y2 As Single = y + bmp.Height
  414.  
  415.         ' Create the points array.
  416.         Dim points() As Point = {New Point(x, y), New Point(x1, y1), New Point(x2, y2)}
  417.         ' Draw the skewed image
  418.         gr.DrawImage(bmp, points)
  419.     End Sub
  420.  
  421.     Sub MakeBitmapSemiTransparent(ByVal bmp As Bitmap, ByVal transparency As Single)
  422.  
  423.     End Sub
  424. End Module
  425.